#!/bin/jruby
require 'java'

java_import org.apache.mahout.cf.taste.model.DataModel
java_import org.apache.mahout.cf.taste.impl.model.file.FileDataModel
java_import org.apache.mahout.cf.taste.impl.recommender.GenericItemBasedRecommender
java_import org.apache.mahout.cf.taste.impl.recommender.GenericUserBasedRecommender
java_import org.apache.mahout.cf.taste.impl.recommender.slopeone.SlopeOneRecommender
java_import org.apache.mahout.cf.taste.impl.similarity.PearsonCorrelationSimilarity
java_import org.apache.mahout.cf.taste.impl.similarity.TanimotoCoefficientSimilarity
java_import org.apache.mahout.cf.taste.impl.neighborhood.NearestNUserNeighborhood;

java_import java.io.File

if ARGV.length < 1
  puts '[error] program argument too less...'
  exit 0
end
workingDir = ARGV[0]

trainDataModal = FileDataModel.new(File.new("#{workingDir}/data/training.csv"))
testDataModal = FileDataModel.new(File.new("#{workingDir}/data/testing.csv"))
perasonSimilarity = PearsonCorrelationSimilarity.new(trainDataModal)
#tanimotoSimilarity = TanimotoCoefficientSimilarity.new(trainDataModal)

# item-based
#pearsonItemRecommender = GenericItemBasedRecommender.new(trainDataModal, perasonSimilarity)
#tanimotoItemRecommender = GenericItemBasedRecommender.new(trainDataModal, tanimotoSimilarity)
#slopeoneRecommender = SlopeOneRecommender.new(trainDataModal)

# user-based
neighborhood = NearestNUserNeighborhood.new(20, perasonSimilarity, trainDataModal)
pearsonUserRecommender = GenericUserBasedRecommender.new(trainDataModal, neighborhood, perasonSimilarity)

hitCnt = 0
totalCnt = 0
userIDItr = testDataModal.getUserIDs
userIDItr.each { |uid|
  recItemList= pearsonUserRecommender.recommend(uid, 10)
  recItemArr = Array.new
  recItemList.each { |item|
    recItemArr.push(item.getItemID());
    #puts item.getValue()
  }
  realItemArr = testDataModal.getPreferencesFromUser(uid).getIDs().to_a
  puts "------------------ #{uid} ------------------"
  puts "recommended: "
  puts recItemArr.inspect()
  puts "real: "
  puts realItemArr.inspect()
  hitsArr = recItemArr & realItemArr
  puts "**************** hits: #{hitsArr.length} *******************" # if hitsArr.length>0
  hitCnt += hitsArr.length
  totalCnt += recItemArr.length
  puts "================ precision: #{hitCnt/totalCnt.to_f}============="
}

